package com.lf.controller;

import com.alibaba.fastjson.JSONObject;
import com.lf.entity.ResponseWrapper;
import com.lf.enums.ErrorCodeEnum;
import com.lf.exception.ApiException;
import com.lf.jwt.CurrentManager;
import com.lf.jwt.ITokenManager;
import com.lf.jwt.SameUrlData;
import com.lf.jwt.TokenEntity;
import com.lf.jwt2admin.ManagerToken;
import com.lf.service.ICustomerService;
import com.lf.service.ServiceList;
import com.lf.service.SysService;
import com.lf.swagger.ApiJsonObject;
import com.lf.swagger.ApiJsonProperty;
import com.lf.util.Asserts;
import io.jsonwebtoken.ExpiredJwtException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @program: 权限接口
 * @description: 用户
 * @author: gxl
 * @create:
 **/
@Slf4j
@Api(value = "customer", tags = "权限")
@RestController
@RequestMapping("/customer")
public class CustomerController extends ServiceList {

    @ManagerToken
    @RequestMapping(value = "/getManagers", method = RequestMethod.POST)
    @ApiOperation(tags = "管理台", httpMethod = "POST", value = "[管理台]获取管理员列表", response = String.class)
    public ResponseWrapper getManagers(@ApiJsonObject(name = "getManagersParams", value = {
            @ApiJsonProperty(key = "page", value = "页码"),
            @ApiJsonProperty(key = "limit", value = "行数")
    }) @RequestBody JSONObject jsonParam) {
        try {
            Integer page = jsonParam.getInteger("page");
            Integer limit = jsonParam.getInteger("limit");
            return customerService.getManagers(page,limit);
        }catch (ApiException e){
            log.error(String.format("[管理台]获取管理员列表失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[管理台]获取管理员列表失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    @ManagerToken
    @RequestMapping(value = "/getRelationUsers", method = RequestMethod.POST)
    @ApiOperation(tags = "管理台", httpMethod = "POST", value = "[管理台]获取权限用户列表", response = String.class)
    public ResponseWrapper getRelationUsers(@ApiJsonObject(name = "getRelationUsersParams", value = {}) @RequestBody JSONObject jsonParam) {
        try {
            return customerService.getRelationUsers();
        }catch (ApiException e){
            log.error(String.format("[管理台]获取权限用户列表失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[管理台]获取权限用户列表失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    @ManagerToken
    @RequestMapping(value = "/relationUser", method = RequestMethod.POST)
    @ApiOperation(tags = "管理台", httpMethod = "POST", value = "[管理台]关联（权限）用户", response = String.class)
    public ResponseWrapper relationUser(@ApiJsonObject(name = "relationUserParams", value = {
            @ApiJsonProperty(key = "adminId", value = "管理员ID"),
            @ApiJsonProperty(key = "permId", value = "权限用户ID")
    }) @RequestBody JSONObject jsonParam) {
        try {
            Integer adminId = jsonParam.getInteger("adminId");
            Long permId = jsonParam.getLong("permId");
            Asserts.notBlank(String.valueOf(adminId),"管理员ID");
            Asserts.notBlank(String.valueOf(permId),"权限用户ID");

            return customerService.relationUser(adminId,permId);
        }catch (ApiException e){
            log.error(String.format("[管理台]关联（权限）用户失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[管理台]关联（权限）用户失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    @SameUrlData
    @ManagerToken
    @RequestMapping(value = "/flushPerm", method = RequestMethod.POST)
    @ApiOperation(tags = "管理台", httpMethod = "POST", value = "[管理台]刷新权限", response = String.class)
    public ResponseWrapper flushPerm(@ApiJsonObject(name = "flushPermParams", value = {}) @RequestBody JSONObject jsonParam) {
        try {
            return customerService.flushPerm();
        }catch (ApiException e){
            log.error(String.format("[管理台]刷新权限失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[管理台]刷新权限失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    @RequestMapping(value = "/addManager", method = RequestMethod.POST)
    @ApiOperation(tags = "管理台", httpMethod = "POST", value = "[管理台]添加管理员", response = String.class)
    public ResponseWrapper addManager(
            @ApiJsonObject(name = "addManagerParams", value = {
                    @ApiJsonProperty(key = "account", value = "账号"),
                    @ApiJsonProperty(key = "password", value = "密码")
            }) @RequestBody JSONObject jsonParam) {
        try {
            String account = jsonParam.getString("account");
            String password = jsonParam.getString("password");
            Asserts.checkAccount(account,"账号");
            Asserts.checkPsw(password,"密码");

            return customerService.addManager(account,password);
        }catch (ApiException e){
            log.error(String.format("[管理台]添加管理员失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[管理台]添加管理员失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

//    @RequestMapping(value = "/setRule", method = RequestMethod.POST)
//    @ApiOperation(tags = "用户", httpMethod = "POST", value = "[前端]更新【说明文本】", response = String.class)
//    public ResponseWrapper setRule(@ApiJsonObject(name = "setRuleParams", value = {
//            @ApiJsonProperty(key = "key", value = "key"),
//            @ApiJsonProperty(key = "value", value = "value")
//    }) @RequestBody JSONObject jsonParam) {
//        try {
//            String key = jsonParam.getString("key");
//            String value = jsonParam.getString("value");
//            sysService.setRule(key, value);
//            return ResponseWrapper.succeed("更新【说明文本】成功");
//        }catch (ApiException e){
//            log.error(String.format("[前端]更新【说明文本】失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
//            return ResponseWrapper.failed(e.getCode(), e.getMessage());
//        }catch (Exception e){
//            log.error(String.format("[前端]更新【说明文本】失败"), e);
//            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
//        }
//    }
//
//    @RequestMapping(value = "/getRule", method = RequestMethod.POST)
//    @ApiOperation(tags = "用户", httpMethod = "POST", value = "[前端]获取【说明文本】", response = String.class)
//    public ResponseWrapper getRule(@ApiJsonObject(name = "getRuleParams", value = {
//            @ApiJsonProperty(key = "key", value = "key")
//    }) @RequestBody JSONObject jsonParam) {
//        try {
//            String key = jsonParam.getString("key");
//            String value = sysService.getRule(key);
//            return ResponseWrapper.succeed(value);
//        }catch (ApiException e){
//            log.error(String.format("[前端]获取【说明文本】失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
//            return ResponseWrapper.failed(e.getCode(), e.getMessage());
//        }catch (Exception e){
//            log.error(String.format("[前端]获取【说明文本】失败"), e);
//            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
//        }
//    }

}
